package com.grantz.function.bbs.core.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.grantz.function.bbs.core.entity.Comment;
import com.grantz.function.bbs.core.inter.CommentService;
import com.grantz.model.bbs.CommentModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/v1/comments")
@Api(value="评论记录",tags={"评论记录接口"})
public class CommentController {

	@Resource
	private CommentService  commentService;

	@ApiOperation(value = "保存")
	@PutMapping
	public ResponseEntity<?>  save(@RequestBody CommentModel model){
		commentService.save(pop(new Comment() ,model));
		return new ResponseEntity<>(HttpStatus.CREATED);
	}
	
	@ApiOperation(value = "更新")
	@PostMapping
	public ResponseEntity<?>  update(@RequestBody CommentModel model){
		Optional<Comment> option = commentService.findOneById(model.getId());
		if(option.isPresent()) {
			commentService.save(pop(option.get() , model));
		}
		return new ResponseEntity<>(HttpStatus.CREATED);
	}
	
	private Comment pop(Comment entity ,CommentModel model) {
		return entity;
	}
}
